﻿
using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using BE;

namespace DA
{
    public class daItem
    {
        public static DataTable ObtenerMateriales(beItem oBE)
        {
            SqlDatabase sqlClient = new SqlDatabase(Configuracion.GetConnection);
            DbCommand dbCmd = sqlClient.GetStoredProcCommand("sp_ConsultarMaterial");
            sqlClient.AddInParameter(dbCmd, "@IDItem", DbType.Int32, oBE.IDItem);
            sqlClient.AddInParameter(dbCmd, "@NroParte", DbType.String , oBE.NroParte);
            sqlClient.AddInParameter(dbCmd, "@Marca", DbType.String , oBE.Marca);
            sqlClient.AddInParameter(dbCmd, "@Categoria", DbType.String , oBE.Categoria);
            sqlClient.AddInParameter(dbCmd, "@Nombre", DbType.String , oBE.Nombre);
            sqlClient.AddInParameter(dbCmd, "@Descripcion", DbType.String , oBE.Descripcion);
            sqlClient.AddInParameter(dbCmd, "@Tipo", DbType.String , oBE.Tipo);
            return sqlClient.ExecuteDataSet(dbCmd).Tables[0];
        }
        public static DataTable ObtenerRepuestos(beItem oBE)
        {
            SqlDatabase sqlClient = new SqlDatabase(Configuracion.GetConnection);
            DbCommand dbCmd = sqlClient.GetStoredProcCommand("sp_ConsultarRepuesto");
            sqlClient.AddInParameter(dbCmd, "@IDItem", DbType.Int32, oBE.IDItem);
            sqlClient.AddInParameter(dbCmd, "@NroParte", DbType.String, oBE.NroParte);
            sqlClient.AddInParameter(dbCmd, "@Marca", DbType.String, oBE.Marca);
            sqlClient.AddInParameter(dbCmd, "@Categoria", DbType.String, oBE.Categoria);
            sqlClient.AddInParameter(dbCmd, "@Nombre", DbType.String, oBE.Nombre);
            sqlClient.AddInParameter(dbCmd, "@Descripcion", DbType.String, oBE.Descripcion);
            sqlClient.AddInParameter(dbCmd, "@Tipo", DbType.String, oBE.Tipo); 
            return sqlClient.ExecuteDataSet(dbCmd).Tables[0];
        }

        public static Int32 Registrar(beItem oBE)
        {
            SqlDatabase sqlClient = new SqlDatabase(Configuracion.GetConnection);
            DbCommand dbCmd = sqlClient.GetStoredProcCommand("sp_RegistrarItem");
            sqlClient.AddInParameter(dbCmd, "@IDItem", DbType.Int32, oBE.IDItem);
            sqlClient.AddInParameter(dbCmd, "@NroParte", DbType.String, oBE.NroParte);
            sqlClient.AddInParameter(dbCmd, "@Marca", DbType.String, oBE.Marca);
            sqlClient.AddInParameter(dbCmd, "@Categoria", DbType.String, oBE.Categoria);
            sqlClient.AddInParameter(dbCmd, "@Nombre", DbType.String, oBE.Nombre);
            sqlClient.AddInParameter(dbCmd, "@Descripcion", DbType.String, oBE.Descripcion);
            sqlClient.AddInParameter(dbCmd, "@Tipo", DbType.String, oBE.Tipo); 
            return Convert.ToInt32(sqlClient.ExecuteScalar(dbCmd));
        }

        public static Int32 Eliminar(beItem oBE)
        {
            SqlDatabase sqlClient = new SqlDatabase(Configuracion.GetConnection);
            DbCommand dbCmd = sqlClient.GetStoredProcCommand("sp_EliminarItem");
            sqlClient.AddInParameter(dbCmd, "@IDItem", DbType.Int32, oBE.IDItem );

            return Convert.ToInt32(sqlClient.ExecuteScalar(dbCmd));
            //sqlClient.ExecuteNonQuery(dbCmd);
        }
    }
}
